package com.focus.xyz.union.pages.backend;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BaseTable;
import com.focus.xyz.core.utils.Constants;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.util.List;

/**
 * Created by chenmeng on 2016/4/26.
 */
public class ProdScope extends BaseTable {
    @FindBy(xpath = "//*[@id=\"set_prod_range\"]")
    private WebElement setProdScope;
    @FindBy(xpath = "//*[@id=\"setSpreadPlatform\"]")
    private WebElement setPlat;

    /*设置产品范围时根据名称输入查询*/
    @FindBy(id = "prodName")
    private WebElement prodNameInput;
    @FindBy(xpath = "//table[@class='tabX']/tbody/tr[3]/td[@colspan='7']")
    private WebElement searchBtn;
    @FindBy(xpath = "//input[@type='button' and @value='提交']")
    private WebElement submitBtn;
    @FindBy(xpath = "//div[@id='prod_range_input_page']/table[@id='pageTable1']/tbody")
    private WebElement prodTable;



    /*
    * 投放平台元素
    * */
    @FindBy(id = "prodPlatform")
    private WebElement platSelect;

    @FindBy(xpath = "//div[@id='prod_platform_input_page']/table[@id='pageTable1']/tbody")
    private WebElement platProdTab;
    @FindBy(xpath = "//button[text()='保存']")
    private WebElement saveBtn;
    @FindBy(xpath = "//input[@type='checkbox']")
    private WebElement checkbox;
    @FindBy(xpath = "//a[text()='全选']")
    private WebElement selectAll;

    /*
    * 设置实际服务费比例
    * */
    @FindBy(xpath = "//*[@id=\"prod_range_detail\"]/tbody")
    private WebElement table;
    @FindBy(xpath = "//a[contains(text(),'设置实际服务费比例')]")
    private WebElement setActualFee;
    @FindBy(id = "prod_service_fee")
    private WebElement feeInput;
    @FindBy(id = "bx_serviceFree_button")
    private WebElement submitFeeBtn;

    public ProdScope(WebDriver driver, Action action){
        super(action);
        PageFactory.initElements(driver,this);
        super.table = table;
    }

    @Override
    protected By getLinkLocator() throws Exception {
        return By.xpath("td[13]/a");
    }

    @Override
    protected By getLocator(int index) throws Exception {
        return By.xpath("td[" + index + "]/div");
    }

    public void linkSetProdScope() throws Exception{
        action.click(setProdScope);
        action.waitForPageLoad();
    }

    public void linkSetPlat() throws Exception{
        action.click(setPlat);
        action.waitForPageLoad();
    }

    /*
    * 搜索产品
    * */
    private void searchProd(String prodName) throws Exception{
        action.inputText(prodNameInput,prodName);
        List<WebElement> inputs = searchBtn.findElements(By.tagName(Constants.TAG_INPUT));
        action.click(inputs.get(2));
    }

    /*
    * 选择产品
    * */
    private void selectProd(String prodName) throws Exception{
        List<WebElement> rows = prodTable.findElements(By.tagName(Constants.TAG_TR));
        OUT:for(WebElement r:rows){
            List<WebElement> cols = r.findElements(By.tagName(Constants.TAG_TD));
            for (WebElement e: cols){
                if(prodName.equals(e.getText())){
                    action.click(cols.get(0).findElement(By.tagName(Constants.TAG_INPUT)));
                    break OUT;
                }
            }
            continue ;
        }
    }

    private void clickSubmitBtn() throws Exception{
        action.click(submitBtn);
    }


    public void setProdScope(String prodName) throws Exception{
        searchProd(prodName);
        selectProd(prodName);
        clickSubmitBtn();
        action.waitForAlert();
        action.acceptAlert();
    }

    private void clickSaveBtn() throws Exception{
        action.click(saveBtn);
        action.waitForAlert();
        action.acceptAlert();
    }

    private void selectPlat(String key) throws Exception{
        action.select(platSelect,key);
    }

    private void selectPlatProd() throws Exception{
        action.selectCheckBox(checkbox);
    }

    private void selectAllProd() throws Exception{
        action.click(selectAll);
    }

    public void setProdPlat(String plat) throws Exception{
        selectPlat(plat);
        selectAllProd();
        clickSaveBtn();
    }

    public void setFee(String fee) throws Exception{
        action.click(setActualFee);
        action.inputText(feeInput,fee);
        action.click(submitFeeBtn);
    }

}
